<?php

require_once 'login.php';
echo <<<_END

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js map-page"> <!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Photogrammar: Advanced Search Results</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="/css/styleMain.css"/>
    <link rel="stylesheet" href="/css/leaflet.css" />
    <!--[if lte IE 8]><link rel="stylesheet" href="http://code.leafletjs.com/leaflet-0.3.1/leaflet.ie.css" /><![endif]-->
    <link  href="/css/cartodb-leaflet.css" rel="stylesheet" type="text/css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/js/leaflet.js"></script>
    <script type="text/javascript" src="/js/wax.leaf.min-6.2.3-touched.js"></script>
    <script type="text/javascript" src="/dist/cartodb-leaflet-min.js"></script>
    <script type="text/javascript" src="/dist/cartodb-popup-min.js"></script>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery.google_menu.js"></script>
    
    <script>
            $('document').ready(function(){
                $('.menu').fixedMenu();
            });
    </script>
    
    <script type="text/javascript" src="../js/mapping.js"></script>
</head>
<body onload="initialize()">

<div id="wrapper" class="clearfix">

<div id="header" class="clearfix">
	<div id="logo">
    	<h1><a href="/"><img src="/images/photogrammar-wordmark.jpg" alt="Yale Photogrammar" title="Yale Photogrammar" /></a></h1>
    </div><!-- /#logo -->
    <nav id="menu" class="menu">
        <ul>
          <li class="single-link">
            <a href="/index.php">Home</a>
            </li>
          <li class="single-link">
            <a href="/map/">Map</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/browse/">Browse</a>
            </li>
          <li class="current"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/search/">Search</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/about/">About</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/blog/">Blog</a>
            </li>
        </ul>
    </nav><!-- /#menu -->
</div><!-- /#header -->

<div id="content-wrapper" class="clearfix">

<div id="results-content" class="clearfix">

<div id="results-header" class="clearfix">
<div id="results-header-toprow">
_END;

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if(!$db_server) die("Unable to connect to MySQL: " .mysql_error());

mysql_select_db($db_database) or die('Unable to connect to MySQL: ' . mysql_error());

$fval = array('pname'=>'', 'month_start'=>'', 'month_stop'=>'', 'year_start'=>'', 'year_stop'=>'', 'van'=>'', 'lot'=>'', 'city'=>'',
                 'county'=>'', 'state'=>'', 'title'=>'', 'start'=>0);
$mons = array('1'=>'January ', '2'=>'February ', '3'=>'March ', '4'=>'April ', '5'=>'May ', '6'=>'June ', '7'=>'July ', '8'=>'August ',
                 '9'=>'September ', '10'=>'October ', '11'=>'November ', '12'=>'December ', '0'=>'');
echo <<<_END

_END;

if(isset($_GET['start']))
{

    $fval['pname'] = $_GET['pname'];
    $fval['month_start'] = $_GET['month_start'];
    $fval['month_stop'] = $_GET['month_stop'];
    $fval['year_start'] = $_GET['year_start'];
    $fval['year_stop'] = $_GET['year_stop'];
    $fval['van'] = $_GET['van'];
    $fval['lot'] = $_GET['lot'];
    $fval['city'] = $_GET['city'];
    $fval['county'] = $_GET['county'];
    $fval['state'] = $_GET['state'];
    $fval['title'] = $_GET['title'];
    $fval['start'] = $_GET['start'];

    $van_code = substr(get_post('van'), 0, 1);
    $van_string = substr(get_post('van'), 1);

    $query = "SELECT * FROM photo2 WHERE pname LIKE '%" . get_post('pname') . "%' ";
/*              "lotnum LIKE '%" . get_post('lot') . "%' AND " . */
	if ($_GET['lot']!=""){     $query .=         "AND lotnum ='" . get_post('lot') . "' ";};
	$query .= "AND city LIKE '%" . get_post('city') . "%' AND " .
             "county LIKE '%" . get_post('county') . "%' AND " .
             "state LIKE '%" . get_post('state') . "%' AND " .
             "title LIKE '%" . get_post('title') . "%' AND " .
             "year >=" . get_post('year_start') . " AND year <=" . get_post('year_stop')  . " AND " .
             "month >=" . get_post('month_start') . " AND month <=" . get_post('month_stop');

    if($van_code == "A") $query = $query . " AND van0='" . $van_string . "'";
    if($van_code == "B") $query = $query . " AND van1='" . $van_string . "'";
    if($van_code == "C") $query = $query . " AND van2='" . $van_string . "'";

    $query = $query . " ORDER BY year, month, cnumber";
    if(get_post('search') != "") {
        $querySearch = "+" . get_post('search');
        $querySearch = str_replace(" ", " +", $querySearch);
        $querySearch = str_replace(" +NOT +", " -", $querySearch);
        $querySearch = str_replace("+NOT +", " -", $querySearch);
        $query = "SELECT * FROM photo2 WHERE MATCH(pname, van0, van1, van2, city, county, state, country, title) AGAINST('" . $querySearch . "' IN BOOLEAN MODE) ";
    }

/*     echo $query; */
    //echo '<br />';
    //$query = "SELECT * FROM photo";
    $result = mysql_query($query);

    if (!$result) die ("Database access failed: " . mysql_error());

    $rows = mysql_num_rows($result);
    if(get_post('search') != "") {
        echo '<div id="results-total"><h2>Search: ' . get_post('search') . '</h2> <span>(';
    } else {
        echo '<div id="results-total"><h2>Advanced Search</h2> <span>(';
    }
    echo $rows . ' photos)</span>';

    //for Photographer:"' . get_post('photographer') . '", Lot = "' .
        //get_post('lotNum') . '"' . '", County = "' .
        //get_post('secondPolitical') . '"'  .  '", Year = "' . get_post('year') . '"' ;
    //if($rows > 200) echo ' ; Showing only first 200 results';
    if($rows != 0) {
    	echo '</div><!--/#results-total--><div id="results-pager"><span>Results: </span>';
    	if(get_post('start') != 0) {
        	$query_arr = $_GET;
        	$query_arr["start"] = max($query_arr["start"] - 60, 0);
        	$query_call = http_build_query($query_arr);
        	echo '<a href="' . 'http://photogrammar.yale.edu/search/results.php?' . $query_call . '">&laquo; </a>';
    	}
        	echo  (get_post('start') + 1)  . '-' . min(get_post('start') + 60, $rows);
    }
        if(get_post('start') + 60 < $rows) {
            $query_arr = $_GET;
            $query_arr["start"] = $query_arr["start"] + 60;
            $query_call = http_build_query($query_arr);
            echo '<a href="' . 'http://photogrammar.yale.edu/search/results.php?' . $query_call . '"> &raquo;</a>'; 
        }

    echo '</div><!--/#results-pager-->';
    echo '</div><!--/#results-header-toprow-->';
    echo '</div><!--/#results-header-->';
    echo '<div id="return-link" class="clearfix"><a href="http://photogrammar.yale.edu/search/">Start New Search</a></div><!--/#return-link-->';
    echo '<div id="results-gallery" class="clearfix">';


    for($j = get_post('start'); $j < min(get_post('start') + 60, $rows); ++$j)
    {
        $pmon =  intval(mysql_result($result, $j, 'month'));
        $pdate = $mons[$pmon] . mysql_result($result, $j, 'year');
        $ptitle = mysql_result($result, $j, 'title');
        if(strlen($ptitle) > 90) {
            $ptitle = substr($ptitle,0,85) . "...";
        }
       echo '<div class="results-container">';
       echo '<div class="results-image"><a href=http://photogrammar.yale.edu/records/index.php?record=' .mysql_result($result, $j, 'cnumber') . '>';
       //echo '<img src="http://lcweb2.loc.gov/service/pnp/fsac/1a34000/1a34300/1a34309r.jpg" height="228" />';
       echo '<img class="results-thumb" src="';
    if (substr(mysql_result($result, $j, 'thumb_url'), -2) != 'NA') {
      if (mysql_result($result, $j, 'thumb_url') == '') {
	      echo '/images/nophoto.png';
      }
      if (mysql_result($result, $j, 'thumb_url') != '') {
  	  	echo 'http://maps.library.yale.edu/images/public/photogrammar/' . mysql_result($result, $j, 'thumb_url');
  	  }
    }
    if (substr(mysql_result($result, $j, 'thumb_url'), -2) == 'NA') {
  	  echo 'http://maps.library.yale.edu/images/public/photogrammar/' . mysql_result($result, $j, 'small_url');
    }
       echo '" />';
       echo '</a></div><!--/.results-image-->';
       echo '<div id="results-meta">';
       echo '<div id="results-title">' . $ptitle . '</div>';
       echo '<div id="results-photographer">' .mysql_result($result, $j, 'pname') . '</div>';
       echo '<div id="results-date">' . $pdate . '</div>';
       if(($j - get_post('start')) % 6 == 5);
       echo '</div><!--/#results-meta-->';
       echo '</div><!--/.results-container-->';
    }
    echo '</div><!--/#results-gallery-->';
    if($rows != 0) {
        echo '<div id="results-footer" class="clearfix"><div id="results-pager"><span>Results: </span>';
        if(get_post('start') != 0) {
            $query_arr = $_GET;
            $query_arr["start"] = max($query_arr["start"] - 60, 0);
            $query_call = http_build_query($query_arr);
            echo '<a href="' . 'http://photogrammar.yale.edu/search/results.php?' . $query_call . '">&laquo; </a>';
        }
    echo  (get_post('start') + 1)  . '-' . min(get_post('start') + 60, $rows);
    }
        if(get_post('start') + 60 < $rows) {
            $query_arr = $_GET;
            $query_arr["start"] = $query_arr["start"] + 60;
            $query_call = http_build_query($query_arr);
            echo '<a href="' . 'http://photogrammar.yale.edu/search/results.php?' . $query_call . '"> &raquo;</a>'; 
    }

    echo '</div><!--/#results-pager--></div><!--/#results-footer-->';

}

mysql_close($db_server);

function get_post($var)
{
    return mysql_real_escape_string($_GET[$var]);
}

echo <<<_END

</div><!--/#results-content-->

</div><!--/#content-wrapper-->

<div id="footer" class="clearfix">
	<div id="institution-logo" class="footer-logo"><a href="http://www.yale.edu">Yale</a></div>
    <a href="http://www.neh.gov/divisions/odh"><div id="partner-logo-01" class="footer-logo"></div></a>
</div><!--/footer -->

</div><!--/#wrapper-->

</body>

</html>
_END;

?>
